package com.ydche.util;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class MD5 {
    public MD5() {
    }

    public static String getMd5(String password, String randomKey) throws NoSuchAlgorithmException {
        String md5 = getMd5(password);
        return getMd5(md5 + randomKey);
    }

    public static String getMd5(String source) throws NoSuchAlgorithmException {
        MessageDigest s = MessageDigest.getInstance("MD5");
        s.update(source.getBytes());
        byte[] bytes = s.digest();
        return StringUtil.toHexString(bytes);
    }

    public static String createMd5Key() {
        Random r = new Random();

        String key;
        for (key = String.valueOf(r.nextInt(10000000)); key.length() < 8; key = "0" + key) {
            ;
        }

        return key;
    }

    public static String encrytion(String text) {
        StringBuffer buf = new StringBuffer("");

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(text.getBytes());
            byte[] b = md.digest();
            byte[] arr$ = b;
            int len$ = b.length;

            for (int i$ = 0; i$ < len$; ++i$) {
                byte aB = arr$[i$];
                int i = aB;
                if (aB < 0) {
                    i = aB + 256;
                }

                if (i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException var9) {
            ;
        }

        return buf.toString();
    }

    public static void main(String[] args) {
        System.out.println(encrytion("123456"));
    }

    public static String getBase64(String str) {
        byte[] b = null;
        String s = null;

        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException var4) {
            var4.printStackTrace();
        }

        if (b != null) {
            s = (new BASE64Encoder()).encode(b);
        }

        return s;
    }

    public static String getFromBase64(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();

            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, "utf-8");
            } catch (Exception var5) {
                var5.printStackTrace();
            }
        }

        return result;
    }

    public static String UMencode(String inputpwd, String id) {
        String key = "UMUW";
        String ecd = null;

        try {
            String randomKey = key + id;
            ecd = getMd5(inputpwd, randomKey);
        } catch (NoSuchAlgorithmException var5) {
            var5.printStackTrace();
        }

        return ecd;
    }
}